package com.lu.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lu.reggie.common.BaseContext;
import com.lu.reggie.common.R;
import com.lu.reggie.entity.ShoppingCart;
import com.lu.reggie.service.IShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 购物车 前端控制器
 * </p>
 *
 * @author 况山云
 * @since 2022-08-22
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private IShoppingCartService iShoppingCartService;

    /**
     * 移动端添加购物车
     * 用户可能会多次添加同一商品
     * 所以应该先判断查找有无
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        log.info("购物车数据为:{}", shoppingCart);
        //点餐用户的id
        Long userId = BaseContext.getCurrentId();
        log.info("用户id为:{}", userId);
        //前端传递无该数据，需要自己添加
        shoppingCart.setUserId(userId);
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, userId);
        //根据套餐或者菜品过滤
        Long dishId = shoppingCart.getDishId();
        log.info("菜品id为:{}", dishId);
        if (dishId != null) {
            //菜品
            queryWrapper.eq(ShoppingCart::getDishId, dishId);
        } else {
            //套餐
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //sql = select * from shopping_cart where userId = ? and dishId = ? or setmealId = ?
        //查询看看有没有
        ShoppingCart cart = iShoppingCartService.getOne(queryWrapper);
        log.info("购物车中有该数据:{}", cart);
        //如果有就+1
        if (cart != null) {
            Integer cartNumber = cart.getNumber();
            cart.setNumber(cartNumber + 1);
            log.info("当前有的数据为:{}", cart);
            iShoppingCartService.updateById(cart);
        } else {
            //没有就给1
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            log.info("新增的数据为:{}", shoppingCart);
            iShoppingCartService.save(shoppingCart);
            cart = shoppingCart;
            log.info("返回的信息为:{}", cart);
        }
        return R.success(cart);

    }


    /**
     * 购物车数据查询
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        log.info("购物车查询!");
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        queryWrapper.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> shoppingCartList = iShoppingCartService.list(queryWrapper);

        return R.success(shoppingCartList);
    }


    /**
     * 根据当前登录用户id清除购物车数据
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean() {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        iShoppingCartService.remove(queryWrapper);
        return R.success("删除成功!");
    }



    @PostMapping("/sub")
    public R<ShoppingCart> remove(@RequestBody ShoppingCart shoppingCart) {
        log.info("删除的菜品和套餐信息为:{}", shoppingCart);
        //去查询该用户删除的该菜品或者套餐信息有几份，多份就-1，只有1就直接删除
        //根据该用户id，dishId，setmealId查询
        Long userId = BaseContext.getCurrentId();
        log.info("当前用户ID为:{}", userId);
        shoppingCart.setUserId(userId);
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, userId);
        //在来根据该用户添加的套餐或者菜品id过滤
        Long dishId = shoppingCart.getDishId();
        log.info("菜品ID为:{}", dishId);
        //判断是套餐还是菜品
        if (dishId != null) {
            //菜品过滤
            queryWrapper.eq(ShoppingCart::getDishId, dishId);
        } else {
            //套餐过滤
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //查询看看有几份
        ShoppingCart cart = iShoppingCartService.getOne(queryWrapper);
        log.info("要删除的菜品或者套餐信息为:{}", cart);
        Integer cartNumber = cart.getNumber();
        log.info("数量为:{}", cartNumber);
        if (cartNumber > 1) {
            cart.setNumber(cartNumber - 1);
            iShoppingCartService.updateById(cart);
        } else {
            iShoppingCartService.removeById(cart);
        }
        return R.success(shoppingCart);
    }
}
